<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class FormAction extends Controller
{
    use \App\Traits\UserTrait, \App\Traits\CompanyTrait, \App\Traits\SessionTrait, \App\Traits\DeviceTrait, \App\Traits\TechTrait, \App\Traits\CaseTrait;
    public function index(Request $request)
    {
        $action = $request->input('action');
        $accountInfo = $this->sessionReturnUserAccountInfo();
        if (!$accountInfo) {
            echo json_encode(['result' => false]);
            exit;
        }

        $accountPhone = $accountInfo['user_phone']; //用户账户号码
        $companyId = $this->sessionReturnCompanyId();
        $requestData1 = [];
        switch ($action) {
            //用户信息
            case 'update_user_pwd': //修改用户密码
                $requestData1 = [
                    'account_phone' => $accountPhone,
                    'old_pwd' => $request->input('old_pwd'),
                    'new_pwd' => $request->input('new_pwd'),
                ];
                $result = $this->userForgetPwdTrait(parent::CODE_UPDATE_USER_PWD, $requestData1);
                break;
            case 'update_user_base_info': //修改基础信息--完成
                $requestData1 = [
                    'account_phone' => $accountPhone,
                    'contact_phone' => $request->input('contact_phone'),
                    'contact_email' => $request->input('contact_email'),
                    'contact_address_id' => $request->input('contact_address_id'),
                    'contact_address' => $request->input('contact_address'),
                ];
                $result = $this->updateUserBaseInfo(parent::CODE_UPDATE_USER_BASE_INFO, $requestData1);
                break;
            //信息认证
            case 'auth_ower': //业主认证
                $requestData1 = [
                    'account_phone' => $accountPhone,
                    'company_id' => $companyId,
                    'company_name' => $request->input('company_name'),
                    'company_type_id' => $request->input('company_type_id'),
                    'logo_info' => $request->input('logo_info') != '' ? $request->input('logo_info') : [],
                    'legan_user_name' => $request->input('legan_user_name'),
                    'legan_user_phone' => $request->input('legan_user_phone'),
                    'legan_user_email' => $request->input('legan_user_email'),
                    'company_regist_date' => $request->input('company_regist_date'),
                    'company_regist_money' => $request->input('company_regist_money'),
                    'company_regist_money_type' => $request->input('company_regist_money_type'),
                    'company_regist_address_id' => $request->input('company_regist_address_id'),
                    'company_regist_address' => $request->input('company_regist_address'),
                    'contact_user_name' => $request->input('contact_user_name'),
                    'contact_user_phone' => $request->input('contact_user_phone'),
                    'fix_phone' => $request->input('fix_phone'),
                    'fix_number' => $request->input('fix_number'),
                    'contact_user_email' => $request->input('contact_user_email'),
                    'zip_code' => $request->input('zip_code'),
                    'contact_user_address_id' => $request->input('contact_user_address_id'),
                    'contact_user_address' => $request->input('contact_user_address'),
                    'company_industry' => $request->input('company_industry', ''),
                    'company_desc' => $request->input('company_desc', ''),
                    'organize_code' => $request->input('organize_code'),
                    'organize_code_file' => $request->input('organize_code_file') != '' ? $request->input('organize_code_file') : [],
                    'organize_regist_number' => $request->input('organize_regist_number'),
                    'organize_regist_number_file' => $request->input('organize_regist_number_file') != '' ? $request->input('organize_regist_number_file') : [],
                ];
                $result = $this->authCompanyOwnerInfo(parent::CODE_AUTH_COMPANY_OWNER_SERVICER_INFO, $requestData1);
                $result2 = false;
                if ($result) {
                    $authUserData = [
                        'company_id' => $companyId,
                    ];
                    $result2 = $this->authUserSubmitCheck(parent::CODE_USER_SUBMIT_AUTH_CHECK, $authUserData);
                }
                $result = $result2;
                break;
            case 'auth_servicer': //服务商认证
                $requestData1 = [
                    'account_phone' => $accountPhone,
                    'company_id' => $companyId,
                    'company_name' => $request->input('company_name'),
                    'company_type_id' => $request->input('company_type_id'),
                    'logo_info' => $request->input('logo_info') != '' ? $request->input('logo_info') : [],
                    'legan_user_name' => $request->input('legan_user_name'),
                    'legan_user_phone' => $request->input('legan_user_phone'),
                    'legan_user_email' => $request->input('legan_user_email'),
                    'company_regist_date' => $request->input('company_regist_date'),
                    'bank_level_id' => $request->input('bank_level_id'),
                    'zip_code' => $request->input('zip_code'),

                    'company_regist_money' => $request->input('company_regist_money'),
                    'company_regist_money_type' => $request->input('company_regist_money_type'),
                    'all_money' => $request->input('all_money'),
                    'earn_money' => $request->input('earn_money'),
                    'company_regist_address_id' => $request->input('company_regist_address_id'),
                    'company_regist_address' => $request->input('company_regist_address'),
                    'contact_user_name' => $request->input('contact_user_name'),
                    'contact_user_phone' => $request->input('contact_user_phone'),
                    'fix_phone' => $request->input('fix_phone'),
                    'fix_number' => $request->input('fix_number'),
                    'contact_user_email' => $request->input('contact_user_email'),
                    'contact_user_address_id' => $request->input('contact_user_address_id'),
                    'contact_user_address' => $request->input('contact_user_address'),
                    //'company_domain_id' => $request->input('company_domain_id'),
                    //'company_service_type_id' => $request->input('company_service_type_id'),
                    'company_domain_id' => trim($request->input('company_domain_id'), ','),
                    'tech_route' => $request->input('tech_route'),

                    'stuff_num' => $request->input('stuff_num'),
                    'tech_num' => $request->input('tech_num'),
                    'major_business' => $request->input('major_business'),
                    'service_domain_id' => $request->input('service_domain_id'),
                    'method_id' => $request->input('method_id'),
                    'organize_code' => $request->input('organize_code'),
                    'organize_code_file' => $request->input('organize_code_file') != '' ? $request->input('organize_code_file') : [],
                    'organize_regist_number' => $request->input('organize_regist_number'),
                    'organize_regist_number_file' => $request->input('organize_regist_number_file') != '' ? $request->input('organize_regist_number_file') : [],

                    'fgw_record' => $request->input('fgw_record') != '' ? $request->input('fgw_record') : [],
                    'gxb_record' => $request->input('gxb_record') != '' ? $request->input('gxb_record') : [],
                    'province_record' => $request->input('province_record') != '' ? $request->input('province_record') : [],
                    'province_record_id' => $request->input('province_record_id'),
                    'city_record' => $request->input('city_record') != '' ? $request->input('city_record') : [],
                    'city_record_id' => $request->input('city_record_id'),

                    'company_banner' => $request->input('company_banner') != '' ? $request->input('company_banner') : [],
                    'company_desc' => $request->input('company_desc', ''),
                    'main_tech_desc' => $request->input('main_tech_desc', ''),
                    'tech_success_desc' => $request->input('tech_success_desc', ''),
                ];
                $result = $this->authCompanyServicerInfo(parent::CODE_AUTH_COMPANY_OWNER_SERVICER_INFO, $requestData1);
                $result2 = false;
                if ($result) {
                    $authUserData = [
                        'company_id' => $companyId,
                    ];
                    $result2 = $this->authUserSubmitCheck(parent::CODE_USER_SUBMIT_AUTH_CHECK, $authUserData);
                }
                $result = $result2;
                break;
            //案例相关
            case 'add_case_info': //新增案例
            case 'edit_case_info': //修改案例
                $requestData1 = [
                    'company_id' => $companyId,
                    'case_name' => $request->input('case_name'),
                    'case_address_id' => $request->input('case_address_id'),
                    'case_domain_id' => $request->input('case_domain_id'),
                    'case_image' => $request->input('case_image'),
                    'case_content' => $request->input('case_content'),
                    'tech_id' => $request->input('tech_id'),
                    'type' => $request->input('type'),
                    'check_type' => $request->input('case_chk', 1), //保存和审核的状态
                ];
                if ($action == 'edit_case_info') {
                    $requestData1['id'] = $request->input('id');
                    $result = $this->editCompanyCaseInfo(parent::CODE_ADD_EDIT_COMPANY_CASE_INFO, $requestData1);
                    $caseId = $requestData1['id'];
                } else {
                    $caseId = $this->addCompanyCaseInfo(parent::CODE_ADD_EDIT_COMPANY_CASE_INFO, $requestData1);
                    $result = $caseId ? true : false;
                }
                if ($requestData1['check_type'] == 2) {
                    $checkData = [
                        'id' => $caseId,
                        'company_id' => $companyId,
                    ];
                    $result = $this->checkCompanyCaseInfo(parent::CODE_CHECK_COMPANY_CASE_INFO, $checkData);
                }
                break;
            case 'del_case_info': //删除案例
                $requestData1 = [
                    'company_id' => $companyId,
                    'id' => $request->input('id'),
                ];
                $result = $this->delCompanyCaseInfo(parent::CODE_DEL_COMPANY_CASE_INFO, $requestData1);
                break;
            case 'check_case_info': //案例审核
                $requestData1 = [
                    'company_id' => $companyId,
                    'id' => $request->input('id'),
                ];
                $result = $this->checkCompanyCaseInfo(parent::CODE_CHECK_COMPANY_CASE_INFO, $requestData1);
                break;
            //技术相关
            case 'add_tech_info': //添加技术
            case 'edit_tech_info': //编辑技术
                $requestData1 = [
                    'company_id' => $companyId,
                    'tech_name' => $request->input('tech_name', ''), //技术名称
                    'tech_domain_id' => $request->input('tech_domain_id_string'), //领域，-分割
                    'tech_product_name' => $request->input('tech_product_name'),
                    'tech_used_range' => $request->input('tech_used_range'),
                    'tech_content' => $request->input('tech_content'), //主要技术内容
                    'patent_info' => $request->input('patent_info', ''),
                    'check_type' => $request->input('tech_chk', 1), //保存和审核的状态
                ];
                if ($action == 'edit_tech_info') {
                    $requestData1['id'] = $request->input('id');
                    $result = $this->editCompanyTechInfo(parent::CODE_ADD_EDIT_COMPANY_TECH_INFO, $requestData1);
                } else {
                    $techId = $this->addCompanyTechInfo(parent::CODE_ADD_EDIT_COMPANY_TECH_INFO, $requestData1);
                    $result = $techId ? true : false;
                }
                if ($requestData1['check_type'] == 2) {
                    $checkData = [
                        'id' => $requestData1['id'],
                        'company_id' => $companyId,
                    ];
                    $result = $this->checkCompanyTechInfo(parent::CODE_CHECK_COMPANY_TECH_INFO, $checkData);
                }
                break;
            case 'check_tech_info': //技术审核
                $requestData1 = [
                    'company_id' => $companyId,
                    'id' => $request->input('id'),
                ];
                $result = $this->checkCompanyTechInfo(parent::CODE_CHECK_COMPANY_TECH_INFO, $requestData1);
                break;
            case 'del_tech_info': //删除技术
                $requestData1 = [
                    'company_id' => $companyId,
                    'id' => $request->input('id'),
                ];
                $result = $this->delCompanyTechInfo(parent::CODE_DEL_COMPANY_TECH_INFO, $requestData1);
                break;
            //设备相关
            case 'add_device_info': //添加设备
            case 'edit_device_info': //修改设备信息
                $requestData1 = [
                    'company_id' => $companyId,
                    'device_name' => $request->input('device_name'),
                    'type_id' => $request->input('type_id'),
                    'brand_id' => $request->input('brand_id'),
                    'img_list' => $request->input('img_list', ''),
                    'device_desc' => $request->input('device_desc'),
                    'check_type' => $request->input('device_chk', 1), //保存和审核的状态
                ];
                if ($action == 'edit_device_info') {
                    $requestData1['id'] = $request->input('id');
                    $result = $this->editCompanyDeviceInfo(parent::CODE_ADD_EDIT_COMPANY_DEVICE_INFO, $requestData1);
                } else {
                    $deviceId = $this->addCompanyDeviceInfo(parent::CODE_ADD_EDIT_COMPANY_DEVICE_INFO, $requestData1);
                    $result = $deviceId ? true : false;
                }
                if ($requestData1['check_type'] == 2) {
                    $checkData = [
                        'id' => $requestData1['id'],
                        'company_id' => $companyId,
                    ];
                    $result = $this->checkCompanyDeviceInfo(parent::CODE_CHECK_COMPANY_DEVICE_INFO, $checkData);
                }
                break;
            case 'check_device_info':
                $requestData1 = [
                    'company_id' => $companyId,
                    'id' => $request->input('id'),
                ];
                $result = $this->checkCompanyDeviceInfo(parent::CODE_CHECK_COMPANY_DEVICE_INFO, $requestData1);
                break;
            case 'del_device_info':
                $requestData1 = [
                    'company_id' => $companyId,
                    'id' => $request->input('id'),
                ];
                $result = $this->delCompanyDeviceInfo(parent::CODE_DEL_COMPANY_DEVICE_INFO, $requestData1);
                break;
            //设备型号相关
            case 'add_device_number_info': //添加设备型号
                $requestData1 = [
                    'company_id' => $companyId,
                    'device_id' => $request->input('device_id'),
                    'param_list_info' => $request->input('param_list_info') != '' ? $request->input('param_list_info') : [],
                ];
                $result = $this->addCompanyDeviceNumberInfo(parent::CODE_ADD_EDIT_COMPANY_DEVICE_NUMBER_INFO, $requestData1);
                break;
            case 'edit_device_number_info': //编辑设备型号
                $requestData1 = [
                    'id' => $request->input('id'),
                    'company_id' => $companyId,
                    'device_id' => $request->input('device_id'),
                    'param_list_info' => $request->input('param_list_info'),
                ];
                $result = $this->editCompanyDeviceNumberInfo(parent::CODE_ADD_EDIT_COMPANY_DEVICE_NUMBER_INFO, $requestData1);
                break;
            case 'del_device_number_info': //删除设备型号
                $requestData = [
                    'device_number_id' => $request->input('id'),
                    'device_id' => $request->input('device_id'),
                    'company_id' => $companyId,
                ];
                $result = $this->delCompanyDeviceNumberInfo(parent::CODE_DEL_COMPANY_DEVICE_NUMBER_INFO, $requestData);
                break;
            //需求相关
            case 'add_needle_info': //发布需求
                $requestData1 = [
                    'company_id' => $companyId,
                ];
                $needleType = $request->input('needle_type');
                if ($needleType == 1) {
                    $requestData1 = [
                        'needle_type' => $needleType,
                        'domain_id' => $request->input('domain_id'),
                        'area_id' => $request->input('area_id'),
                    ];
                } else if ($needleType == 2) {
                    $requestData1 = [
                        'needle_type' => $needleType,
                        'domain_id' => $request->input('domain_id'),
                        'area_id' => $request->input('area_id'),
                    ];
                } else if ($needleType == 4) {
                    $requestData1 = [
                        'needle_type' => $needleType,
                        'domain_id' => $request->input('domain_id'),
                        'tech_id' => $request->input('tech_id'),
                    ];
                } else if ($needleType == 3) {
                    $requestData1 = [
                        'needle_type' => $needleType,
                        'money_used_id' => $request->input('money_used_id'),
                        'money_sum_id' => $request->input('money_sum_id'),
                        'project_type_id' => $request->input('project_type_id'),
                        'area_id' => $request->input('area_id'),
                    ];
                } else if ($needleType == 0) {
                    $requestData1 = [
                        'needle_type' => $needleType,
                    ];
                }
                $requestData2 = [
                    'contact_user_name' => $request->input('contact_user_name'),
                    'contact_user_phone' => $request->input('contact_user_phone'),
                    'content' => $request->input('content'),
                ];

                $requestData = array_merge($requestData1, $requestData2);
                $result = $this->addNeedleInfo(parent::CODE_ADD_NEEDLE_INFO, $requestData);
                break;
            default:
                # code...
                break;
        }

        echo $result ? json_encode(['result' => 0]) : json_encode(['result' => -1]);
    }

}
